﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using DomainModelDemo.Model.Repository;
using DomainModelDemo.Model;
using DomainModelDemo.Infrastructure.Exceptions;
using DomainModelDemo.Infrastructure.Helpers;
using System.Data;
using System.Data.SqlClient;
namespace DomainModelDemo.Repository
{
    public class PostRepository:IPostRepository
    {
        private string connectionString = string.Empty;
        public PostRepository(string connectionString)
        {
            this.connectionString = connectionString;
        }
        public void Add(Post post)
        {
            if (post == null)
            {
                throw new ArgumentNullException("post");
            }
            try
            {
                string sqlstr = "Insert Into Posts(PostId,Title,PostContent,Author,PublishDate,IsPublished)Values(@PostId,@Title,@PostContent,@Author,@PublishDate,@IsPublished)";
                SqlParameter[] parameters = { 
                                                new SqlParameter("@PostId", post.PostId),
                                                new SqlParameter("@Title",post.Title),
                                                new SqlParameter("@PostContent",post.PostContent),
                                                new SqlParameter("@Author",post.Author),
                                                new SqlParameter("@PublishDate",post.PublishDate),
                                                new SqlParameter("@IsPublished",post.IsPublished)                                            
                                            };
                SqlHelper.ExecuteNonQuery(connectionString,CommandType.Text,sqlstr,parameters);
            }
            catch(Exception ex)
            {
                throw new Exception<PostExceptionArgs>(new PostExceptionArgs("Add"), ex.Message);
            }
        }

        public void Update(Post post)
        {
            if (post == null)
            {
                throw new ArgumentNullException("post");
            }
            try
            {
                //const string sqlstr = "insert into Db_Blog () Values ()";
                throw new NotImplementedException();  //db.ExecuteNonQuery(CommandType.Text, sqlstr);
            }
            catch (Exception ex)
            {
                throw new Exception<PostExceptionArgs>(new PostExceptionArgs("Update"), ex.Message);
            }
        }

        public void Delete(Post post)
        {
            if (post == null)
            {
                throw new ArgumentNullException("post");
            }
            try
            {
               
            }
            catch (Exception ex)
            {
                throw new Exception<PostExceptionArgs>(new PostExceptionArgs("Delete"), ex.Message);
            }
        }

        public IList<Post> FindAll()
        {
            throw new NotImplementedException();
          
        }

        public Post FindBy(Guid postId)
        {
            if (string.IsNullOrEmpty(postId.ToString()))
            {
                throw new ArgumentNullException("postId");
            }
            string sqlstr = "select * From Posts Where PostId=@PostId";
            SqlParameter para = new SqlParameter("@PostId", postId);
            SqlDataReader myreader=SqlHelper.ExecuteReader(connectionString, CommandType.Text, sqlstr,para);

        }

        public IList<Post> FindByPaged(int size, int index, out int total)
        {
            throw new NotImplementedException();
        }
    }
}
